﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class ListItemDetails : System.Web.UI.Page
{
    SqlConnection conn = null;
    string idrequest = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["Employee"] == null)
            {
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                conn = Database.GetConnection();
                idrequest = Request.QueryString["idrequest"].ToString();
                GetListItem();
                ChangeStatus();
            }
            
        }
        catch(Exception ex)
        {
            Response.Redirect("ListRequest.aspx");
        }


    }
    public void GetListItem()
    {
        string query = "select ItemDetails.Detail_ID, Item.I_Name, ItemDetails.R_ID, ItemDetails.Quantity from ItemDetails inner join Item on ItemDetails.I_ID = Item.I_ID where ItemDetails.R_ID = "+idrequest+"";
        SqlDataAdapter adapter = new SqlDataAdapter(query,conn);
        DataTable dt = new DataTable();
        adapter.Fill(dt);
        gvListItem.DataSource = dt;
        gvListItem.DataBind();
    }
    protected void txtSubmit_Click(object sender, EventArgs e)
    {
        if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
            
        }
        string choicestatus = ddlStatus.SelectedValue.ToString();
        if (choicestatus.CompareTo("Choice") == 0)
        {
            lblError.Text = "Please choose status";
        }
        else
        {
            SqlCommand comm = new SqlCommand("update Request set R_Status = '"+choicestatus+"' where R_ID = "+idrequest+" ",conn);
            comm.ExecuteNonQuery();
            Response.Redirect("ListRequest.aspx");
        }
        
    }
    public void ChangeStatus()
    {
        SqlDataAdapter adapter = new SqlDataAdapter("select * from Request where R_ID = "+idrequest+"",conn);
        DataTable dt = new DataTable();
        adapter.Fill(dt);
        string status = dt.Rows[0][4].ToString();
        if(status.CompareTo("Withdraw")==0)
        {
            ddlStatus.Items.RemoveAt(1);
        }
    }
}